package com.zdsoft.finance.product.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.zdsoft.finance.product.entity.ProductFunction;
import com.zdsoft.framework.core.common.domain.BaseEntity;
import com.zdsoft.framework.core.common.util.ObjectHelper;

/**
 * 部门功能操作仓库自定义实现
 * @author longwei
 * @date 2016/11/22
 * @version 1.0
 */
public class ProductFunctionRepositoryImpl {
	//查询头部
	private StringBuffer hqlBuffer = new StringBuffer("from ProductFunction where 1=1 ");
	
	@PersistenceContext
	private EntityManager em;
	
	/**
	 * 通过对象查询分页
	 * @param productFunction 查询对象
	 * @param pageable 分页信息
	 * @return 分页对象
	 */
	@SuppressWarnings("unchecked")
	public Page<ProductFunction> queryByCons(ProductFunction productFunction, Pageable pageable){
		//条件组装
		StringBuffer whereHqlBuffer = new StringBuffer(" and isDeleted=:isDeleted");
		if(ObjectHelper.isNotEmpty(productFunction.getOrgCd())){
			whereHqlBuffer.append(" and orgCd = :orgCd ");
		}
		if(ObjectHelper.isNotEmpty(productFunction.getFunctionCd())){
			whereHqlBuffer.append(" and functionCd = :functionCd");
		}
		if(ObjectHelper.isNotEmpty(productFunction.getIsApplayForm())){
			whereHqlBuffer.append(" and isApplayForm = :isApplayForm");
		}
		if(ObjectHelper.isNotEmpty(productFunction.getUrlKey())){
			whereHqlBuffer.append(" and urlKey like :urlKey");
		}
		if(ObjectHelper.isNotEmpty(productFunction.getProcessNm())){
			whereHqlBuffer.append(" and processNm like :processNm");
		}
		if(ObjectHelper.isNotEmpty(productFunction.getProduct()) && ObjectHelper.isNotEmpty(productFunction.getProduct().getId())){
			whereHqlBuffer.append(" and product.id = :productId");
		}
		whereHqlBuffer.append(" order by createTime desc");
		
		//参数组装
		Query query = em.createQuery(this.hqlBuffer.toString() + whereHqlBuffer.toString());
		Query countQ = em.createQuery("select count(*) " + this.hqlBuffer.toString() + whereHqlBuffer.toString());
		query.setParameter("isDeleted",!BaseEntity.DELETED);
		countQ.setParameter("isDeleted", !BaseEntity.DELETED);
		
		if(ObjectHelper.isNotEmpty(productFunction.getOrgCd())){
			query.setParameter("orgCd",productFunction.getOrgCd());
			countQ.setParameter("orgCd", productFunction.getOrgCd());
		}
		if(ObjectHelper.isNotEmpty(productFunction.getFunctionCd())){
			query.setParameter("functionCd",productFunction.getFunctionCd());
			countQ.setParameter("functionCd", productFunction.getFunctionCd());
		}
		if(ObjectHelper.isNotEmpty(productFunction.getIsApplayForm())){
			query.setParameter("isApplayForm",productFunction.getIsApplayForm());
			countQ.setParameter("isApplayForm", productFunction.getIsApplayForm());
		}
		if(ObjectHelper.isNotEmpty(productFunction.getUrlKey())){
			query.setParameter("urlKey","%"+productFunction.getUrlKey()+"%");
			countQ.setParameter("urlKey","%"+productFunction.getUrlKey()+"%");
		}
		if(ObjectHelper.isNotEmpty(productFunction.getProcessNm())){
			query.setParameter("processNm","%"+productFunction.getProcessNm()+"%");
			countQ.setParameter("processNm","%"+productFunction.getProcessNm()+"%");
		}
		if(ObjectHelper.isNotEmpty(productFunction.getProduct()) && ObjectHelper.isNotEmpty(productFunction.getProduct().getId())){
			query.setParameter("productId",productFunction.getProduct().getId());
			countQ.setParameter("productId",productFunction.getProduct().getId());
		}
		
		//查询
		query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
		query.setMaxResults(pageable.getPageSize());
		Page<ProductFunction> pager = new PageImpl<>(query.getResultList(),pageable,(Long)countQ.getSingleResult());
		return pager;
	}
}
